package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void addMember(Member member) {
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCount(List<String> monthsList) {
        List<Integer> memberCount = new ArrayList<>();
        //根据每月的月份查询当月之前(包括当月的会员数量)
        for (String month : monthsList) { //此时的month格式为2020.03
            //在原来的month基础上，拼接最后一天，查询数据库时，只需要判断此日期之前的就可以了
            String date = month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);

            memberCount.add(count);
        }
        return memberCount;
    }


}
